package com.mnm.asynctaskmanager.core;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;

public abstract class Task extends AsyncTask<String, TaskProgress, Integer> {

    /**
     * @uml.property  name="mResult"
     */
    private Integer m_result;
    /**
     * @uml.property  name="mProgressMessage"
     */
    private TaskProgress m_progressMessage;
    public TaskProgress getM_progressMessage() {
		return m_progressMessage;
	}

	/**
     * @uml.property  name="mProgressTracker"
     * @uml.associationEnd  
     */
    protected IProgressTracker m_progressTracker;

    @Override
    protected abstract Integer doInBackground(String... arg0);
   
    @Override
    protected void onPostExecute(Integer result) {
            // Update result
            m_result = result;
            // And send it to progress tracker
            if (m_progressTracker != null) {
                m_progressTracker.onComplete();
            }
            // Detach from progress tracker
            m_progressTracker = null;
    }

    public Task(TaskProgress message) {
            m_progressMessage = message;
    }

    public void setProgressTracker(IProgressTracker progressTracker) {
            // Attach to progress tracker
            m_progressTracker = progressTracker;
            // Initialise progress tracker with current task state
            if (m_progressTracker != null) {
                    m_progressTracker.onProgress(m_progressMessage);
                    if (m_result != null) {
                            m_progressTracker.onComplete();
                    }
            }
    }

    @Override
    protected void onCancelled() {
            // Detach from progress tracker
            m_progressTracker = null;
    }

    @Override
    protected void onProgressUpdate(TaskProgress... values) {
            // Update progress message
            m_progressMessage = values[0];
            // And send it to progress tracker
            if (m_progressTracker != null) {
                    m_progressTracker.onProgress(m_progressMessage);
            }
    }
}
